<?php

/**
 * This is the model class for table "news".
 *
 * The followings are the available columns in table 'news':
 * @property integer $id
 * @property integer $project_id
 * @property string $title
 * @property string $summary
 * @property string $description
 * @property integer $author_id
 * @property string $created_on
 * @property integer $comments_count
 *
 * The followings are the available model relations:
 * @property Project $project
 * @property User $author
 */
class News extends CActiveRecord
{
    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return News the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'news';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('project_id, author_id, comments_count', 'numerical', 'integerOnly'=>true),
            array('title', 'length', 'max'=>60),
            array('summary', 'length', 'max'=>255),
            array('description, created_on', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, project_id, title, summary, description, author_id, created_on, comments_count', 'safe', 'on'=>'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'project' => array(self::BELONGS_TO, 'Project', 'project_id'),
            'author' => array(self::BELONGS_TO, 'User', 'author_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'project_id' => 'Project',
            'title' => 'Title',
            'summary' => 'Summary',
            'description' => 'Description',
            'author_id' => 'Author',
            'created_on' => 'Created On',
            'comments_count' => 'Comments Count',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('id',$this->id);
        $criteria->compare('project_id',$this->project_id);
        $criteria->compare('title',$this->title,true);
        $criteria->compare('summary',$this->summary,true);
        $criteria->compare('description',$this->description,true);
        $criteria->compare('author_id',$this->author_id);
        $criteria->compare('created_on',$this->created_on,true);
        $criteria->compare('comments_count',$this->comments_count);

        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
        ));
    }
}